package com.tanhua.server.service;

import com.tanhua.dubbo.api.MeasuringSoulApi;
import com.tanhua.model.dto.AnswerDTO;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.ExamVO;
import com.tanhua.model.vo.ReportVO;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 测灵魂service
 */
@Service
public class MeasuringSoulService {

    @DubboReference
    private MeasuringSoulApi measuringSoulApi;

    /**
     * 查询试卷列表
     * @return
     */
    public List<ExamVO> findExamListWithUser() {
        //查询数据库表试题列表--本次开发不随机抽取，而是直接读取数据库中的记录
        //如果该用户已经回答完毕某一套试卷，则记录其报告。并且按照其回答过的试卷的level来设定后面的试卷是否开放
        Long userId = UserHolder.getUserId();
        List<ExamVO> examVOList = measuringSoulApi.selectExamWithUser(userId);
        return examVOList;
    }

    public String commitAnswers(Map<String,List<AnswerDTO>> answerMap) {
        Long userId = UserHolder.getUserId();
        List<AnswerDTO> answers = answerMap.get("answers");
        String reportId = measuringSoulApi.saveAnswerAndReport(answers,userId);
        return reportId;
    }

    public ReportVO selectReportByReportId(Integer id) {

        ReportVO reportVO = measuringSoulApi.selectReportById(id,UserHolder.getUserId());
        if(reportVO != null)
            return reportVO;
        else
            throw new BusinessException(ErrorResult.builder().errMessage("报告跑丢了~~").build());
    }
}
